/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.marknum.importsolr.demos.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson2.JSON;
import com.marknum.importsolr.bean.DateTEst;
import com.marknum.importsolr.bean.mn_ztk_aj_xsajjbxx;
import com.marknum.importsolr.enu.TagEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Slf4j
@RestController
public class BasicController {
    //    @Autowired
//    Http2SolrClient http2SolrClient;
    @Autowired
    SolrTemplate solrTemplate;

    @PostMapping("/import")
    public String hello(@RequestParam MultipartFile file, @RequestParam @Nullable String sheet) throws SolrServerException, IOException, ClassNotFoundException {
        if (StrUtil.isNotBlank(sheet)) {
            if (TagEnum.valueOf(sheet) == null) {
                return "表格不存在";
            }
            simpleRead(file, sheet);
        } else {
            for (TagEnum value : TagEnum.values()) {
                simpleRead(file, value.name());
            }
        }
        return "import success ";
    }

    public <T> void simpleRead(MultipartFile file, String className) throws IOException, ClassNotFoundException {
        Class<?> collectionClass = Class.forName("com.marknum.importsolr.bean." + className);
        Class<?> excelClass = Class.forName("com.marknum.importsolr.exceldata." + className + "_excel");

        log.info("插入{}数据", className);
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
        // 具体需要返回多少行可以在`PageReadListener`的构造函数设置
        EasyExcel.read(file.getInputStream(), excelClass, new PageReadListener<T>(dataList -> {

            String jsonString = JSON.toJSONString(dataList);
            log.info("读取到一条数据{}", jsonString);
            List bean = JSON.parseArray(jsonString, collectionClass);
            solrTemplate.saveBeans(className, bean);
            // 表名即类名
            solrTemplate.commit(className);

        })).password("110").headRowNumber(2).sheet(className).doRead();

    }

    @PostMapping("/clear/{collection}")
    public String clear(@PathVariable String collection) {

        if (TagEnum.valueOf(collection) == null) {
            return "表格不存在";
        }

        SimpleQuery query = new SimpleQuery("*:*");
        solrTemplate.delete(collection, query);
        solrTemplate.commit(collection);
        return "clear success";
    }

    @PostMapping("/clear")
    public String clear() {
        for (TagEnum tagEnum : TagEnum.values()) {
            SimpleQuery query = new SimpleQuery("*:*");
            solrTemplate.delete(tagEnum.name(), query);
            solrTemplate.commit(tagEnum.name());
        }
        return "clear success";
    }

}
